/*
 * TimeLabel.java
 *
 * Created on September 2, 2006, 3:35 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.bldc.jflubber;

import com.sun.java.swing.SwingUtilities2;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @author gfx
 */
public class TimeLabel extends JLabel {
    private static boolean isInitialized = false;
    private static BufferedImage background = null;
    private static ShadowFactory factory = new ShadowFactory(4, 0.65f, Color.BLACK);
    
    private boolean textChanged = false;
    private BufferedImage textImage = null;
    private BufferedImage shadow = null;

    /** Creates a new instance of TimeLabel */
    public TimeLabel() {
        setFont(new Font("Arial", Font.PLAIN, 36));
    }
    
    public TimeLabel(String text) {
        super(text);
        setFont(new Font("Arial", Font.PLAIN, 36));
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        
        if (!isInitialized) {
            try {
                background = ImageIO.read(getClass().getResource("images/metal.png"));
                isInitialized = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        if (isInitialized) {
            g2d.drawImage(background, 0, 0, null);
        }
        
        if (getText() !=  null) {
            if (textChanged || textImage == null) {
                FontMetrics metrics = getFontMetrics(getFont());
                int width = metrics.stringWidth(getText());

                textImage = new BufferedImage(width, metrics.getHeight(),
                        BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2 = textImage.createGraphics();
                g2.setFont(getFont());
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g2.setColor(Color.WHITE);
                g2.drawString(getText(), 0, metrics.getAscent());
                g2.dispose();

                shadow = factory.createShadow(textImage);
                
                textChanged = false;
            }
            
            double x = ((double) (getWidth() - textImage.getWidth())) / 2.0d;
            double y = ((double) (getHeight() - textImage.getHeight())) / 2.0d;
            g2d.translate(x, y);
            g2d.drawImage(shadow, -2, 2, null);
            g2d.drawImage(textImage, 0, 0, null);
            g2d.translate(-x, -y);
        }
    }
    
    @Override
    public void setText(String text) {
        super.setText(text);
        textChanged = true;
    }
}
